package com.tanhua.server.controller;

import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.SettingsVo;
import com.tanhua.server.interceptor.UserHolder;
import com.tanhua.server.service.SettingsService;
import com.tanhua.server.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @author SunSun
 * @date 2021/10/24
 */
@RestController
@RequestMapping("/users")
public class SettingsController {

    @Autowired
    private SettingsService settingsService;

    @Autowired
    private UserService userService;

    /**
     * 通用设置信息
     * @return
     */
    @GetMapping("/settings")
    public ResponseEntity settings(){

        SettingsVo vo=settingsService.settings();

        return ResponseEntity.ok(vo);
    }

    /*
    陌生人问题设置
     */
    @PostMapping("/questions")
    public ResponseEntity questions(@RequestBody Map map){

        String content = (String) map.get("content");

        settingsService.saveQuestion(content);

        return ResponseEntity.ok(null);
    }

    /*
    通知设置
     */
    @PostMapping("/notifications/setting")
    public ResponseEntity setting(@RequestBody Map map){
        settingsService.saveSettings(map);
        return ResponseEntity.ok(null);
    }

    /**
     * 获得黑名单
     * @param page
     * @param pagesize
     * @return
     */
    @GetMapping("/blacklist")
    public ResponseEntity  blacklist(@RequestParam(defaultValue = "1") int page,
                                     @RequestParam(defaultValue = "10")int pagesize){

        PageResult pr= settingsService.blacklist(page,pagesize);

        return ResponseEntity.ok(pr);

    }

    /**
     * 删除黑名单
     * @param uid
     * @return
     */
    @DeleteMapping("/blacklist/{uid}")
    public ResponseEntity deleteBlackList(@PathVariable("uid") Long uid){

        settingsService.deleteBlackList(uid);

        return ResponseEntity.ok(null);

    }

    /**
     * 修改手机号
     * @return
     */
    @PostMapping("/phone/sendVerificationCode")
    public ResponseEntity  sendPhone(){

        String mobile = UserHolder.getMobile();
        userService.sendMsg(mobile);
        return ResponseEntity.ok(null);
    }

    /*
    修改校验验证码
     */
    @PostMapping("/phone/checkVerificationCode")
    public Map code(@RequestBody Map map){

        String phone = UserHolder.getMobile();

        String code = (String) map.get("verificationCode");

        Map map1=settingsService.checkVerificationCode(phone,code);

        return map1;
    }

    /**
     * 修改后保存手机号
     * @param map
     * @return
     */
    @PostMapping("/phone")
    public ResponseEntity savePhone(@RequestBody Map map){

        String phone = (String) map.get("phone");

        Long userId = UserHolder.getUserId();

        settingsService.savePhone(phone,userId);

        return ResponseEntity.ok(null);
    }

}
